package com.lee.wordcounter.utils;

import com.lee.wordcounter.WordCounter;
import com.lee.wordcounter.impl.ArrayListWordCounter;
import com.lee.wordcounter.impl.MapWordCounter;

public class TestRunner {
    static final String ARRAYLIST_STORE = "ArrayListWC--store";
    static final String ARRAYLIST_RETRIEVE = "ArrayListWC--retrieve";
    static final String [] ARRAYLIST_KEYS = {ARRAYLIST_STORE, ARRAYLIST_RETRIEVE};
    static final String MAP_STORE = "ArrayListWC--store";
    static final String MAP_RETRIEVE = "ArrayListWC--retrieve";
    static final String [] MAP_KEYS = {MAP_STORE, MAP_RETRIEVE};    
	   
	public String [] runArrayListImpl() {
	       WordCounter wc = new ArrayListWordCounter();
	       Timer.startTimer();
	       wc.processWords(WordCounterUtils.testFileName);
	       Timer.endTimer();
	       Timer.storeReading(ARRAYLIST_KEYS[0]);
	       Timer.startTimer();
	       String [] topTen = wc.getTopRankedWords(10);
	       Timer.endTimer();
	       Timer.storeReading(ARRAYLIST_KEYS[1]);	
	       return topTen;
	}
	
	public String [] runMapImpl() {
	       WordCounter wc = new MapWordCounter();
	       Timer.startTimer();
	       wc.processWords(WordCounterUtils.testFileName);
	       Timer.endTimer();
	       Timer.storeReading(MAP_KEYS[0]);
	       Timer.startTimer();
	       String [] topTen = wc.getTopRankedWords(10);
	       Timer.endTimer();
	       Timer.storeReading(MAP_KEYS[1]);	
	       return topTen;
	}	
	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub


		   //WordCounterUtils.generateRandomTextFile(testFile);
		   System.gc();
	       Thread.sleep(10000);		
	       
           TestRunner tr = new TestRunner();
           String [] result = tr.runArrayListImpl();
	       for (String s : ARRAYLIST_KEYS) {
	    	   System.out.println(s + " : " + Timer.getReading(s));
	       }
	       System.gc();
	       Thread.sleep(10000);
	       String [] resultMap = tr.runMapImpl();
	       for (String s : MAP_KEYS) {
	    	   System.out.println(s + " : " + Timer.getReading(s));
	       }	       
	       int index  = 0;
	       for (String s : result) {
	    	   System.out.println(index + ": " + s + " | " + resultMap[index]);
	    	   index ++;
	       }
	}

}
